const http = require('../../utils/http.js');

Page({
  data: {
    id_number: '',
    name: '',
    genderOptions: ['男', '女'],
    genderIndex: null,
    birth_date: '',
    ethnicityOptions: [], // 民族选项
    ethnicityIndex: null,
    politicalStatusOptions: [], // 政治面貌选项
    politicalStatusIndex: null,
    foreignLanguageOptions: [], // 外语选项
    foreignLanguageIndex: null,
    phone: '',
    password: '',
    recordedText: '' // 录音结果
  },

  onLoad() {
    // 获取民族、政治面貌和外语数据
    this.fetchOptions('/ethnicity/list', 'ethnicityOptions');
    this.fetchOptions('/politicalstatus/list', 'politicalStatusOptions');
    this.fetchOptions('/foreignlanguage/list', 'foreignLanguageOptions');
  },

  fetchOptions(endpoint, stateKey) {

    http.get(endpoint).then(res =>{
      if (res.data && res.data.code === 200) {
        this.setData({ [stateKey]: res.data.data });
      }
    }).catch(err=>{
      console.log(err);
    })
  },
  handleDateChange(e) {
    this.setData({
      birth_date: e.detail.value // 直接绑定选中的日期
    });
  },
  handlePickerChange(e) {
    const { field } = e.currentTarget.dataset;
    this.setData({
      [`${field}Index`]: e.detail.value
    });
  },

  handleInput(e) {
    const { field } = e.currentTarget.dataset;
    this.setData({
      [field]: e.detail.value
    });
  },
  onRecordStart() {
    // 语音识别功能的添加
    wx.startRecord({
      success: (res) => {
        this.setData({ recordedText: res.tempFilePath });
      },
      fail: () => {
        wx.showToast({ title: '录音失败', icon: 'none' });
      }
    });
  },
  handleSubmit(){
    const base_candidate_info = {
      id_number: this.data.id_number,
      name: this.data.name,
      gender: this.data.genderOptions[this.data.genderIndex] || '', // 获取选中的性别值
      birth_date: this.data.birth_date,
      ethnicity_id: this.data.ethnicityOptions[this.data.ethnicityIndex].id, // 多选项的ID拼接为字符串
      political_status_id: this.data.politicalStatusOptions[this.data.politicalStatusIndex].id, // 多选项的ID拼接为字符串
      foreign_language_id: this.data.foreignLanguageOptions[this.data.foreignLanguageIndex].id, // 多选项的ID拼接为字符串
      phone: this.data.phone,
      password: this.data.password
    };
    http.post("/candidate/register",base_candidate_info).then(res=>{
      console.log(res);
      if(res.data.code == 200){
        // wx.setStorageSync('base_candidate_info', base_candidate_info)
        wx.showToast({
          title: '注册成功',
          duration:2000
        })
        // 跳转页面
        wx.redirectTo({
          url: `/pages/login/login?id_number=${this.data.id_number}&password=${this.data.password}`,
        })
      }
    }).catch(err=>{
      console.log(err);
      wx.showToast({
        title: '注册失败',
        duration:2000
      })
    })
  },


  onSubmit() {
    const { id_number, name, genderIndex, birth_date, phone, password } = this.data;
    const errors = [];

    if (!id_number) errors.push('身份证号不能为空');
    // else if (id_number.length !== 18) errors.push('身份证号必须为18位');

    if (!name) errors.push('姓名不能为空');

    if (genderIndex === null) errors.push('请选择性别');

    if (!birth_date) errors.push('请选择出生日期');

    if (!phone) errors.push('手机号不能为空');

    if (!password) errors.push('密码不能为空');
    else if (password.length < 6) errors.push('密码长度必须大于6位');

    if (errors.length > 0) {
      wx.showToast({ title: errors[0], icon: 'none' });
      return;
    }
  },

  onRecordStart() {
    wx.startRecord({
      success: (res) => {
        this.setData({ recordedText: res.tempFilePath });
      },
      fail: () => {
        wx.showToast({ title: '录音失败', icon: 'none' });
      }
    });
  }
});